/**
	Copyright (c) 2009 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 12/28/2009
*/
@isTest
private class SelectContactControllerTester {
	private static List<Contact> contacts;
	private static List<User> users;
	private static SelectContactController controller;
	private static PageReference nextPage;
		
	private static void testInit() {
		List<Contact> newContacts = new List<Contact>();
		String cName = 'TestContactOnly_';
		String cIngenioName = 'IngenioTestOnly_';
		String cEmail = 'contact@ingenio.com';
		for(Integer i=0 ; i < 10 ; i++){
			Contact c = new Contact(LastName = cName + i);
			c.Ingenio_UserName__c = cIngenioName + i;
			c.Ingenio_UserID__c = '' + i;
			c.Email = cEmail + i;
			newContacts.add(c);
		}
		insert newContacts;
		contacts = [select c.Id, c.Name, c.Email, c.Ingenio_UserID__c, c.Ingenio_UserName__c 
		            from Contact c where Name like: cName + '%'];
		System.assertEquals(10, contacts.size());
		
		List<User> newUsers = new List<User>();
		String uName = 'TestUserOnly_'; 
		String uEmail = 'user@ingenio.com'; 
		ID profileId = [select Id from Profile limit 1].Id;
		for(Integer i=0 ; i < 10 ; i++){
			User u = new User(LastName = uName + i);
			u.Ingenio_User_ID__c = '' + i;
			u.Email = '_' + i + '_' + uEmail;
			u.Username = 'john@acme.com' + i;
			u.Alias = 'nick' + i;
			u.CommunityNickname = 'community' + i;
			u.TimeZoneSidKey = 'America/Los_Angeles';
			u.LocaleSidKey = 'en_US';
			u.EmailEncodingKey = 'UTF-8';
			u.ProfileId = profileId;
			u.LanguageLocaleKey = 'en_US';
			newUsers.add(u);
		}
		insert newUsers;
		users = [select u.Name, u.Email, u.Ingenio_User_ID__c from User u where u.Name like :uName + '%'];
		System.assertEquals(10 , users.size());    	
	}
	
	public static testMethod void testSearchContactandUser(){
		testInit();
		PageReference pageRef = Page.SelectContactPage;
		Test.setCurrentPage(pageRef);
		
		controller = new SelectContactController();
		System.assertEquals('Look Up Contact', controller.getTitle());
		System.assertEquals('Contact Selector', controller.getBlockTitle());
		System.assertEquals(0, controller.lContact.size());
		System.assertEquals(0, controller.lUser.size());
		
		System.assertEquals('Contact', controller.selectedObjTypeName);
		controller.searchName = '';
		controller.go();
		System.assertEquals(false, controller.lContact.isEmpty());		
		
		controller.searchName = 'TestContactOnly_';
		controller.go();
		System.assertEquals(10, controller.lContact.size());
		
		controller.searchName = 'xyxyxyxyxyxyxyxyxyxxxxx19900';
		controller.go();
		System.assertEquals(0, controller.lContact.size());
		
		// switch object type from Contact to User	
		controller.selectedObjTypeName = 'User';
		controller.switchObjType();
		
		controller.searchName = '';
		controller.go();
		System.assertEquals(false, controller.lUser.isEmpty());		
		
		controller.searchName = 'TestUserOnly_';
		controller.go();
		System.assertEquals(10, controller.lUser.size());
		
		controller.searchName = 'xyxyxyxyxyxyxyxyxyxxxxx19900';
		controller.go();
		System.assertEquals(0, controller.lUser.size());
		
		controller.selectedObjTypeName = 'Contact';	
		controller.switchObjType();
		System.assertEquals(true, controller.isContact);
		System.assertEquals(false, controller.isUser);
	}
	
	public static testMethod void testAddccBccAdditionalTo(){
		testInit();
		PageReference pageRef = Page.SelectCcBccAdditionalToPage;
		Test.setCurrentPage(pageRef);
		
		controller = new SelectContactController();
		String refcc, refBcc, refAdditionalTo;
		controller.additionalToEmail = '';
		controller.bccEmail = '';
		controller.ccEmail = '';
		controller.selectedsobjtEmail = 'xyz@att.com'; 
		refAdditionalTo = controller.selectedsobjtEmail;
		refBcc = controller.selectedsobjtEmail;
		refcc = controller.selectedsobjtEmail;
		controller.addAdditionalTo();
		controller.addBCC();
		controller.addCC();
		System.assertEquals(refAdditionalTo, controller.additionalToEmail);
		System.assertEquals(refBcc, controller.bccEmail);
		System.assertEquals(refcc, controller.ccEmail);
		
		controller.selectedsobjtEmail = 'abc@att.com'; 		
		refAdditionalTo = controller.additionalToEmail + ';' + controller.selectedsobjtEmail;
		refBcc = controller.bccEmail + ';' + controller.selectedsobjtEmail;
		refcc = controller.ccEmail + ';' + controller.selectedsobjtEmail;
		controller.addAdditionalTo();
		controller.addBCC();
		controller.addCC();
		System.assertEquals(refAdditionalTo, controller.additionalToEmail);
		System.assertEquals(refBcc, controller.bccEmail);
		System.assertEquals(refcc, controller.ccEmail);
	}
}